<?php

class Model_Institution extends Model_Dec {

	const TABLE = "institution";
	
	/**
	 * @var protected $id id value
	 * @method int get_id() get_id(void)
	 * @method void set_id() set_id(int $id)
	 */
	protected $id;
	
	/**
	 * @var protected $university_name university_name value
	 * @method string get_university_name() get_university_name(void)
	 * @method void set_university_name() set_university_name(string $university_name)
	 */
	protected $university_name;
	
	/**
	 * @var protected $institute_name institute_name value
	 * @method string get_institute_name() get_institute_name(void)
	 * @method void set_institute_name() set_institute_name(string $institute_name)
	 */
	protected $institute_name;
	
	/**
	 * @var protected $city city value
	 * @method string get_city() get_city(void)
	 * @method void set_city() set_city(string $city)
	 */
	protected $city;
	
	/**
	 * @var protected $state state value
	 * @method string get_state() get_state(void)
	 * @method void set_state() set_state(string $state)
	 */
	protected $state;
	
	/**
	 * @var protected $user_id user_id value
	 * @method int get_user_id() get_user_id(void)
	 * @method void set_user_id() set_user_id(int $user_id)
	 */
	protected $user_id;

	/**
	 * @return Model_Institution
	 */
	public function __construct($id=0) {
		if(0<intval($id)) {
			$db = DB::select("*")->from(self::TABLE)->where("id", "=", $id)->limit(1);
			$db = $db->as_assoc()->execute();
			if($db->count()>0) {
				$this->load($db->current());
			}
		}
	}

	protected static function get_database_fields() {
		return array(
			'university_name' => __("Institution_University_name"),
			'institute_name' => __("Institution_Institute_name"),
			'city' => __("Institution_City"),
			'state' => __("Institution_State"),
			'user_id' => __("Institution_User_id"),
		);
	}

	public function validate() {
		$validateArray = array();
		foreach (self::get_database_fields() as $key => $value) {
			$validateArray[$key] = $this->get($key);
		}
		$validation = Validation::factory($validateArray)
				->rule('university_name', 'not_empty')
				->rule('institute_name', 'not_empty')
				->rule('city', 'not_empty')
				->rule('state', 'not_empty')
				->rule('user_id', 'not_empty')
				;
		if ($validation->check()) {
			return $validateArray;
		} else {
			throw new Validation_Exception('validation', $validation);
		}
	}

	public static function get_report() {
		return DB::select(
				"id"
				, "university_name"
				, "institute_name"
				, "city"
				, "state"
				, "user_id"
				, array("id", "actions"))
		->from(self::TABLE)
		->order_by("id", "ASC");
	}
        public static function get_user_institution($user_id,$only_id=true){
            $id = DB::select("id")->from(self::TABLE)->where("user_id","=", $user_id)->as_object()->execute();
            if($only_id){
                return $id->get("id");
            }else{
                if($id->count()>0) {
                    return new Model_Institution($id->get("id"));
                }else{
                    return 0;
                }
            }
        }

}
